//学生用来进行修改个人信息
$(document).ready(function () {

    $.validator.setDefaults({
        errorClass: "uk-text-danger",
        errorPlacement: function (error, element) {
            error.insertAfter(element.parent("div"));
        },
        highlight: function (element, errorClass, validClass) {
            $(element).addClass("uk-form-danger");
        },
        unhighlight: function (element, errorClass, validClass) {
            $(element).removeClass("uk-form-danger");
        }
    });
    StudentInfoDialog.setupFormValidation();
});
var blockingModal;
function searchStudent() {
    $.ajax({
        url: "/api/student/search_student",
        method: 'GET',
        dataType: 'json'
    })
        .done(function (student) {
            //alert(student);
            if (student!= null) {
                fillupInfo(student);
            } else {
                UIkit.notify("未找到用户");
            }
        })
        .fail(function (jqXHR) {
            try {
                var response = JSON.parse(jqXHR.responseText);
                if (response != null && response.error != null) {
                    UIkit.modal.alert("发生错误: " + response.error.message);
                } else {
                    UIkit.modal.alert("发生错误。");
                }
            } catch (e) {
                console.error(e);
                UIkit.modal.alert("发生错误。")
            }
        })
        .always(function () {
            blockingModal.hide();
        });
}

function fillupInfo(student) {
    $('#account_user_type')[0].value = 'S';
    $('#student_no')[0].value = student.studentNumber;
    $('#student_name')[0].value = student.name;
    $('#student_birth_date')[0].value = objectToString(student.birthDate);
    $('#student_gender')[0].value = student.gender;
    $('#student_id_number')[0].value = student.idNumber;
    $('#student_phone')[0].value = objectToString(student.phone);
    $('#student_college')[0].value = objectToString(student.collegeId, 0);
    $('#student_department')[0].value = objectToString(student.departmentId, 0);
    $('#student_enrollment_year')[0].value = student.enrollmentYear;
}

function onAccountSave() {
    var userType = $('#account_user_type')[0].value;
    var newAccount = { userType: userType };
    var form;
    if (userType === 'S') {
        form = $('#form_student_info');
        if (form.valid()) {
            var student = form.serializeJSON({ skipFalsyValuesForTypes: ['number', 'string'] });
            student.studentNumber = $('#student_no')[0].value;
            newAccount.student = student;
            //alert(newAccount);
        } else {
            return;
        }
    } else
        return ;
    blockingModal = UIkit.modal.blockUI('正在提交修改...');
    $.ajax({
        url: '/api/admin/update_account',
        data: JSON.stringify(newAccount),
        contentType: 'application/json',
        type: 'PUT'
    })
        .done(function () {
            blockingModal.hide();
            UIkit.notify('修改成功！', { status: 'success'});
        })
        .fail(function (jqXHR) {
            try {
                var response = JSON.parse(jqXHR.responseText);
                if (response != null && response.error != null) {
                    UIkit.modal.alert("发生错误: " + response.error.message);
                } else {
                    UIkit.modal.alert("发生错误。");
                }
            } catch (e) {
                console.error(e);
                UIkit.modal.alert("发生错误。");
            }
        })
        .always(function () {
            blockingModal.hide();
        });
}
function objectToString(o, defaultVal) {
    if (defaultVal == null)
        defaultVal = "";
    return o == null ? defaultVal : o.toString();
}